package com.zklh.leetcode.interviewTop150;

/**
 * 跳跃游戏II算法实现
 *
 * @Author tongnan
 * @Date 2024/5/21
 * @Version 1.0
 */
public class Solution45 {
    public int jump(int[] nums) {
        if (nums.length == 1) {
            return 0;
        }
        int farthest = nums[0];
        int current = 0;
        int next = 0;
        int step = 0;
        while (farthest < nums.length - 1) {
            for (int i = current+1; i <= current + nums[current]; i++) {
                if (farthest <= i + nums[i]) {
                    next = i;
                    farthest = i + nums[i];
                }
            }
            step++;
            current = next;
        }
        return step+1;
    }

    public static void main(String[] args) {
        Solution45 solution45 = new Solution45();
        int[] nums = {1,1,1,1,1};
        System.out.println(solution45.jump(nums));
    }
}